<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once 'biff-reader/CompoundDocument.inc.php';
require_once 'biff-reader/BiffWorkbook.inc.php';

class XLSReader {
	var $CI       ='';
	var $hojas    =array();
	var $libro    =array();

	function __construct()
	{
		$CI = & get_instance();
	}

	function read($fileName){
		if (!is_readable ($fileName)) die ('Cannot read ' . $fileName);
		$doc = new CompoundDocument ('utf-8');
		$doc->parse (file_get_contents ($fileName));
		$wb = new BiffWorkbook ($doc);
		$wb->parse ();
		$i=0;
		foreach ($wb->sheets as $sheetName => $sheet)
		{
			$this->hojas[$i]=$sheetName;
			for ($row = 0; $row < $sheet->rows(); $row ++)
			{
				for ($col = 0; $col < $sheet->cols (); $col ++)
				{
					if (isset ($sheet->cells [$row][$col])){
						$cell = $sheet->cells [$row][$col];
						if(!empty($cell->value)){

							$this->libro[$i][$row][$col]=$cell->value;
						}

					}
				}
			}
			$i++;
		}
	}

	function libro($hoja=false,$rowdesde=0,$rowhasta=50000,$coldesde=0,$colhasta=false){
		$output=array();

		if(isset($this->libro[$hoja]))
			$output=$this->libro[$hoja];
		else
			$output=$this->libro;

		$output2=array();
		foreach($output as $key=>$value){
			if($key>=$rowdesde && $key <=$rowhasta)
			$output2[$key]=$value;
		}
		return $output2;
	}

	function hojas($hoja=false){
		if(isset($this->hojas[$hoja]))
			return $this->hojas[$hoja];
		else
			return $this->hojas;
	}
}
